package com.atguigu.gmall.product.utils;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;


/**
 * @Author: 孙润达
 * @Date: 2022/1/5 21:09
 */
//@Component将FileUploadUtils类放入到string容器中
@Component
public class FileUploadUtils {
    /*执行顺序
    1.FileUploadUtils类
    2.构造器
    3，static
    4.注入
    5.后置处理器
    * */
      private static TrackerClient trackerClient=null;
      @Value("${tracker_server}")//ip
      private String TRACKERSERVER;
      @Value("${connect_timeout}")//超时时间
      private  Integer CONNECTTIMEOUT;
      @Value("${network_timeout}")//网络超时时间
      private  Integer NETWORKTIMEOUT;
      @Value("${charset}")//编码集
      private  String CHARSET;

      //Spring Ioc容器后置处理'
    @PostConstruct
    private  void init(){
        try {
            InetSocketAddress[] tracker_servers = new InetSocketAddress[1];

            String[] parts = TRACKERSERVER.split("\\:", 2);
            InetSocketAddress inetSocketAddress =
                    new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
            tracker_servers[0]=inetSocketAddress;
            TrackerGroup g_tracker_group = new TrackerGroup(tracker_servers);
            ClientGlobal.setG_tracker_group(g_tracker_group);
            //超时时间
            ClientGlobal.setG_connect_timeout(CONNECTTIMEOUT);
            //网络超时时间
            ClientGlobal.setG_network_timeout(NETWORKTIMEOUT);
            ////编码集
            ClientGlobal.setG_charset(CHARSET);
            //1创建跟踪器
            trackerClient = new TrackerClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  String upLoad(String fileName,byte[] file){

        //加载配置文件
        try {
            //2.连接跟踪器
            TrackerServer connection = trackerClient.getConnection();
            //3.创建服务器的储存节点客户端
            StorageServer storageServer=null;
            StorageClient1 storageClient1 = new StorageClient1(connection, storageServer);
            //获取文件的后缀名
            String ext = FilenameUtils.getExtension(fileName);
            //上传图片
         return storageClient1.upload_file1(fileName, ext, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public   byte[] downLoad(String fileId){
        //加载配置文件
        try {
            //2.连接跟踪器
            TrackerServer connection = trackerClient.getConnection();
            //3.创建服务器的储存节点客户端
        /*
        StorageClient1有两个方法，无参和两参
        * */
            StorageServer storageServer=null;
            StorageClient1 storageClient1 = new StorageClient1(connection, storageServer);
            //下载
           return storageClient1.download_file1(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
